package com.opennews.openplatform.familyexpenseapi.controller

import com.opennews.openplatform.familyexpenseapi.common.constant.IncomeExpenseType
import com.opennews.openplatform.familyexpenseapi.common.constant.PaymentType
import com.opennews.openplatform.familyexpenseapi.config.AppConfigProperties
import com.opennews.openplatform.familyexpenseapi.service.AccountGroupService
import com.opennews.openplatform.familyexpenseapi.service.BankCardService
import com.opennews.openplatform.familyexpenseapi.service.IncomeExpenseDetailTypeService
import com.opennews.openplatform.myspringbootcore.dto.ApiResponse
import com.opennews.openplatform.myspringbootcore.util.getSuccessResponse
import com.opennews.openplatform.myspringbootmvccore.security.RequestIdentityExtractor
import org.springframework.core.env.Environment
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/data-initialization")
class DataInitializationController(
    private val appConfigProperties: AppConfigProperties,
    private val accountGroupService: AccountGroupService,
    private val bankCardService: BankCardService,
    private val incomeExpenseDetailTypeService: IncomeExpenseDetailTypeService,
    private val requestIdentityExtractor: RequestIdentityExtractor,
) {
    @GetMapping("/retrieve-all")
    fun retrieveAll(): ResponseEntity<ApiResponse<Map<String, Any?>>> {
        val claims = requestIdentityExtractor.getClaims()
        val accountGroupId = claims.accountGroupId.asString()
        val userId = claims.id.asString()
        val accountGroupInfo = accountGroupService.queryAccountGroupInfo(accountGroupId, userId)
        val bankCards = bankCardService.queryAllWithBasicInfoByAccountGroup(accountGroupId)
        val detailTypes = incomeExpenseDetailTypeService.queryByAccountGroup(accountGroupId, null)

        val incomeExpenseType = mapOf(
            IncomeExpenseType.EXPENSE to "支出",
            IncomeExpenseType.INCOME to "收入"
        )

        val paymentType = mapOf(
            PaymentType.CASH to "现金",
            PaymentType.CREDIT_CARD to "信用卡"
        )

        val result = mapOf(
            "accountGroupInfo" to accountGroupInfo,
            "bankCards" to bankCards,
            "detailTypes" to detailTypes,
            "incomeExpenseType" to incomeExpenseType,
            "paymentType" to paymentType,
            "recentSearchListMaxCount" to appConfigProperties.appConfig.recentSearchListMaxCount,
            "baiduMap" to mapOf(
                "androidKey" to appConfigProperties.baiduMap.androidKey
            ),
            "amap" to mapOf(
                "androidKey" to appConfigProperties.amap.androidKey,
                "webApiKey" to appConfigProperties.amap.webApiKey
            )
        )

        return getSuccessResponse(result)
    }
}